package org.xiaom.butler.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class UserFilter implements Filter {
	private static final String LOGIN_PATH = "login.jsp";  
	private static final String USER = "user";  

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpReq = (HttpServletRequest) request;
		HttpServletResponse httpResp = (HttpServletResponse) response;
		String currentUrl = httpReq.getRequestURI();
		String targetUrl = currentUrl.substring(currentUrl.indexOf("/", 1), 
				currentUrl.length()); 
		HttpSession session=httpReq.getSession(false);
		
		if("/login.jsp".equals(targetUrl)){
			
		}else if("/login.do".equals(targetUrl)){
			
		}else{
			if(session==null||session.getAttribute("user")==null){
			//	httpResp.sendRedirect(httpReq.getContextPath() + "/login.jsp");
			//	return;
			}
		}
		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
	}

}
